// pages/goods_detail/index.js
Page({
  data: {
    goodsObj:{},
    isCollect:false
  },
  GoodsInfo:{},
  handleCollect(){
    let isCollect = false;
    let collect = wx.getStorageSync("collect") || [];
    let index = collect.findIndex(v => v.goods_id === this.GoodsInfo.goods_id);
    if(index!=-1){
      collect.splice(index,1);
      isCollect = false;
      wx.showToast({
        title: '取消成功',
        icon:'success',
        mask:true
      });
    }else{
      collect.push(this.GoodsInfo);
      isCollect = true;
      wx.showToast({
        title: '收藏成功',
        icon:'success',
        mask:true
      })
    }
    wx.setStorageSync("collect", collect);
    this.setData({
      isCollect
    })
  },
  /**
   * 页面的初始数据
   */
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.goods_id);
    this.getGoodsDetail(options.goods_id);
  },
  getGoodsDetail(goods_id){
    wx.request({
      url: 'https://api-hmugo-web.itheima.net/api/public/v1/goods/detail',
      data:{
        goods_id:goods_id
      },
      success:res=>{
        console.log(res.data.message);
        this.setData({
          goodsObj:res.data.message
        })
        this.GoodsInfo = this.data.goodsObj
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let pages = getCurrentPages();
    let currentPage = pages[pages.length - 1];
    let options = currentPage.options;
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})